package com.example.day0210;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.day0210.R;
import com.example.day0210.adapter.MyFragmentAdapter;
import com.example.day0210.fragment.Fragment1;
import com.example.day0210.fragment.Fragment2;
import com.example.day0210.fragment.M1Fragment;
import com.example.day0210.fragment.M2Fragment;
import com.example.day0210.fragment.M3Fragment;
import com.example.day0210.fragment.M4Fragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private RadioGroup rg;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private ArrayList<Fragment> list = new ArrayList();
    private MyFragmentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);

        list.add(new M1Fragment());
        list.add(new M2Fragment());
        list.add(new M3Fragment());
        list.add(new M4Fragment());

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (rb1.getId() == checkedId) {
                    vp.setCurrentItem(0);
                } else if (rb2.getId() == checkedId) {
                    vp.setCurrentItem(1);
                } else if (rb3.getId() == checkedId) {
                    vp.setCurrentItem(2);
                } else if (rb4.getId() == checkedId) {
                    vp.setCurrentItem(3);
                }
            }
        });

        adapter = new MyFragmentAdapter(getSupportFragmentManager(),list);
        vp.setAdapter(adapter);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    rb1.setChecked(true);
                }
                if (i == 1) {
                    rb2.setChecked(true);
                }
                if (i == 2) {
                    rb3.setChecked(true);
                }
                if (i == 3) {
                    rb4.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });




    }
}
